import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @program: mybatisUtil
 * @description:
 * @author: fengleifeng
 * @create: 2020-06-18 11:26
 **/
public class EasyExcelUtil {


    /**
     * 将数据转成json
     *
     * @param dataSource
     * @param head
     * @return
     */
    private static String getMapToStr(Map<Integer, String> dataSource, Map<Integer, String> head) {
        Map<String, Object> result = new HashMap<>(2);
        Map<String, String> data = new HashMap<>(head.size());
        result.put("eventId", "eventId");
        Iterator<Map.Entry<Integer, String>> iterator = head.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Integer, String> map = iterator.next();
            if (dataSource.containsKey(map.getKey())) {
                data.put(map.getValue(), dataSource.get(map.getKey()));
            } else {
                data.put(map.getValue(), "");
            }
        }
        result.put("data", data);
        return new Gson().toJson(result);
    }

    public static void main(String[] args) {
        Gson gson = new Gson();
String headstr = "{\"0\":\"userid\",\"1\":\"phoneno\",\"2\":\"comment\"}";
        Map<Integer, String> head  = gson.fromJson(headstr,HashMap.class);
        String datastr = "{\"0\":\"1\",\"1\":\"11\"}";
        Map<Integer, String> data  = gson.fromJson(datastr,HashMap.class);

        System.out.println(getMapToStr(data,head));
    }
}
